package com.woniuxy.cq.phrase3.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniuxy.cq.phrase3.common.Result;
import com.woniuxy.cq.phrase3.common.ValidationUtil;
import com.woniuxy.cq.phrase3.controller.form.UpdateProjectForm;
import com.woniuxy.cq.phrase3.dao.ProjectMapper;
import com.woniuxy.cq.phrase3.entity.Project;
import com.woniuxy.cq.phrase3.entity.Sbp;
import com.woniuxy.cq.phrase3.entity.Sx;
import com.woniuxy.cq.phrase3.service.IProjectService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Date;

/**
 * @author Daniel
 * @version 1.0.0
 * @ClassName ApplicationController
 * @Description TODO
 * @createTime 2020年09月09日 10:46:00
 */
@RestController
@Slf4j
public class ApplicationController {

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private IProjectService projectService;
    /**
     * @description: 分页查询
     * @param: pageIndex
     * @param: pageSize
     * @param: session
     * @param: projectPhase
     * @return: com.woniuxy.cq.phrase3.common.Result
     * @author Daniel
     * @date: 2020/9/9 12:13
     */
    @GetMapping("/application/queryAll")
    @RequiresRoles("applicant")
    public Result select(@RequestParam(defaultValue = "1") int pageIndex, @RequestParam(defaultValue = "5") int pageSize, HttpSession session, String projectPhase) {

        log.info("传入的阶段{}", projectPhase);

        Integer userId = (Integer)session.getAttribute("userId");
        System.out.println("-----------------------------------------------------"+userId);
        IPage<Project> sxIpage = projectService.pageQuery2(pageIndex, pageSize, userId, projectPhase);
        return Result.success(sxIpage);
    }
    /**
     * @description: 用户更新项目内容
     * @param: projectId
     * @param: projectContent
     * @return: com.woniuxy.cq.phrase3.common.Result
     * @author Daniel
     * @date: 2020/9/9 14:29
     */
    @PostMapping("/application/update")
    @RequiresRoles("applicant")
    public Result update(@RequestBody @Valid UpdateProjectForm updateProjectForm ,BindingResult result){
        if(result.hasErrors()){
            String validationResult = ValidationUtil.extract(result);
            log.info("校验失败:{}",validationResult);
            return Result.fail("ILLEGAL_ARGUMENTS",validationResult);
        }
        log.info("传入的id为:{},传入的项目内容为:{},修改的名字为:{}",updateProjectForm.getProjectContent(),updateProjectForm.getProjectId(),updateProjectForm.getProjectId());
        Project project = new Project();
        project.setProjectId(updateProjectForm.getProjectId());
        project.setProjectContent(updateProjectForm.getProjectContent());
        project.setProjectName(updateProjectForm.getProjectName());
        int rows = projectMapper.updateById(project);
        if (rows!=0){
            return Result.success();
        }else {
            return Result.fail("500","更改失败");
        }
    }


}
